#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct ListNode
{
    int val;
    struct ListNode* next;
};

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* cur1 = list1, * cur2 = list2;
    struct ListNode* Newhead = NULL;
    struct ListNode* tmp = NULL;

    if (cur1 == NULL && cur2 == NULL)
        return Newhead;
    if (cur1 == NULL && cur2 != NULL)
        return list2;
    if (cur1 != NULL && cur2 == NULL)
        return list1;
    if (cur1->val < cur2->val)
    {
        Newhead = cur1;
        cur1 = cur1->next;
    }
    else
    {
        Newhead = cur2;
        cur2 = cur2->next;
    }
    tmp = Newhead;
    while (cur1 && cur2)
    {
        if (cur1->val < cur2->val)
        {
            tmp->next = cur1;
            cur1 = cur1->next;
            tmp = tmp->next;
        }
        else
        {
            tmp->next = cur2;
            cur2 = cur2->next;
            tmp = tmp->next;
        }
    }
    if (cur1)
    {
        while (cur1)
        {
            tmp->next = cur1;
            cur1 = cur1->next;
            tmp = tmp->next;
        }
    }
    if (cur2)
    {
        while (cur2)
        {
            tmp->next = cur2;
            cur2 = cur2->next;
            tmp = tmp->next;
        }
    }
    tmp = NULL;
    return Newhead;
}

